import java.util.AbstractQueue;
import java.util.Iterator;


public class Buffer {
	private Package buff[]=null;
	private int pleft=0;
	private int pright=0;
	private int size; 

	public Buffer(int size){
		this.size=size;
		buff=new Package[size];
	}
	
	
	public void add(Package p) throws BufferOverflowException {
		if (pright<size-1){ //if we still have capacity
			buff[pright]=p;
			pright++;
		}
		else {
			throw new BufferOverflowException();			
		}
	}

	
	public Package peek() {
		return buff[pleft]; //primul element din coada e cel mai vechi 0<-1
	}

	public Package get() throws EmptyBufferException{
		if (pright>0){
		Package tmp=peek();
		for (int i=0; i<pright; i++){
			buff[i]=buff[i+1];
		}
//		buff[pright]=null;
		pright--;
		return tmp;
		}
		else{
			//throw new EmptyBufferException();
			return null;
		}
	}


	public int size() {
		return size;
	}
	
	public boolean hasSpace(){
		return pright<size-1;
	}
	
	public String toString(){
		String s="Vechi ";
		for (int i=pleft; i<pright;i++){
			s+=buff[i]+" ";
		}
		s+=" Nou\n";
		return s;
	}

}
